<?php


require_once 'jujucock.php';
require_once 'optparse.php';
require_once 'logger.php';


class   EchoServer  extends Server
{
    public function serve($service, $record, $address_from)
    {
        $logger = getLogger();
        $logger->debug(sprintf('SERVICE "%s" serve "%s" from "%s"', $service, $record, $address_from), __FILE__, __LINE__);
        
        $address_to = $address_from;
        return array($service, $record, $address_to);
    }   // serve()
}   // class EchoServer

define('HOST', '127.0.0.1');
define('PORT', 54344);
define('SERVICE', 'ECHO');

$opt_parser = new OptionParser();
$opt_parser->addOption('-v', '--verbose', 'verbose', 'show verbose information', 'bool');
try {
	list($options, $args) = $opt_parser->parse( $argv );
}
catch(Exception $ex) {
	echo 'OptionParser Exception: ',$ex->getMessage(),"\n";
	exit(2);
}

$logger = getLogger();
if (isset($options['verbose']) && $options['verbose']) {
    $logger->setLogLevel(TinyLogger::LEVEL_DEBUG);
}

$jube = new Jujube();
$jube->listenTCP( PORT );

$server = new EchoServer();
$jube->registerService(SERVICE, $server);

$jube->run();

